package cc.yugu.yhdw.webserver.service.basic;

import cc.yugu.yhdw.common.domain.basic.AdImage;
import cc.yugu.yhdw.common.entity.json.ExtResult;
import cc.yugu.yhdw.common.entity.pagination.Page;
import cc.yugu.yhdw.common.utils.HttpClientUtils;
import cc.yugu.yhdw.common.utils.YhdwUtils;
import cc.yugu.yhdw.webserver.config.AppConfig;
import cc.yugu.yhdw.webserver.persistence.basic.AdImageMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
 * 广告图片业务
 * 
 * @Date:Created in 2018/7/2 16:45
 * @Author:
 * @Modified By: zjn 2018/7/2 16:45
 */ 
@Service
public class AdImageService {

    final static String AD_IMAGE_URL = "/security/basic_upload/ad_image.htm";

    @Autowired
    AppConfig config;

    @Autowired
    AdImageMapper adImageMapper;
    /**
    * 根据主键查询表bas_ad_image
    * 
    * @Param: [id]
    * @Return: cc.yugu.yhdw.common.domain.basic.AdImage
    * @Author By: zjn 2018/7/2  17:03
    */ 
    public AdImage find(int id) {
        return adImageMapper.find(id);
    }
    /**
    * 分页查询
    * 
    * @Param: [search]
    * @Return: cc.yugu.yhdw.common.entity.pagination.Page
    * @Author By: zjn 2018/7/2  17:05
    */ 
    public Page findPage(AdImage search) {
        Page page = search.buildPage();
        page.setTotalItems(adImageMapper.findPageCount(search));
        search.setBeginIndex(page.getOffset());
        page.setResult(adImageMapper.findPageResult(search));
        return page;
    }
    /**
    * 往表中插入的 图片地址和编号
    *
    * @Param: [entity, fileMap]
    * @Return: int
    * @Author By: zjn 2018/7/2  17:54
    */
    public int create(AdImage entity, Map<String, File> fileMap) {

        if (!fileMap.isEmpty()) {
            Map<String, String> paramMap = new HashMap<String, String>();
            // String.format("%d",x) :格式转换，将x转换成十进制的格式 ，再以key-value的格式保存到集合中
            paramMap.put("adImageId", String.format("%d", entity.getId()));

            HttpClientUtils.HttpResp httpResp = HttpClientUtils.uploadFile(config.staticUrl + AD_IMAGE_URL, fileMap, paramMap, YhdwUtils.buildHttpHeader(System.currentTimeMillis(), config.uploadSalt));
            Map map = null;
            try {
                map = (Map) YhdwUtils.decodeJson(httpResp.content, Map.class);
            } catch (IOException e) {
                e.printStackTrace();
            }
            Map data = (Map) map.get("data");

            String imagePath = (String) data.get("filePath");
            if (StringUtils.isNotEmpty(imagePath)) {
                entity.setImagePath(imagePath);
            }
        }
        return adImageMapper.insert(entity);
    }
    /**
    * 根据主键更新表
    * 
    * @Param: [entity, fileMap]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/2  18:08
    */ 
    public ExtResult update(AdImage entity, Map<String, File> fileMap) throws IOException {
        if (!fileMap.isEmpty()) {
            Map<String, String> paramMap = new HashMap<String, String>();
            paramMap.put("adImageId", String.format("%d", entity.getId()));

            HttpClientUtils.HttpResp httpResp = HttpClientUtils.uploadFile(config.staticUrl + "/security/upload/ad_image.htm", fileMap, paramMap, YhdwUtils.buildHttpHeader(System.currentTimeMillis(), config.uploadSalt));
            Map map = (Map) YhdwUtils.decodeJson(httpResp.content, Map.class);
            Map data = (Map) map.get("data");

            String imagePath = (String) data.get("filePath");
            if (StringUtils.isNotEmpty(imagePath)) {
                entity.setImagePath(imagePath);
            }
        }

        adImageMapper.update(entity);
        return ExtResult.successResult();
    }
    /**
    * 根据主键 更新订单编号
    * 
    * @Param: [id, orderNum]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/2  18:08
    */ 
    public ExtResult updateOrderNum(int id, int orderNum) {
        adImageMapper.updateOrderNum(id, orderNum);
        return ExtResult.successResult();
    }
    /**
    * 根据主键删除
    * 
    * @Param: [id]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/2  18:10
    */ 
    public ExtResult delete(int id) {
        adImageMapper.delete(id);
        return ExtResult.successResult();
    }

}
